package service.comment;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import object.Comment;

import service.CommonServlet;

public class GetComments extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			int sId = Integer.parseInt( request.getParameter("sId") );
			if(sId==0) throw new Exception("sId can't be empty");
			String sql = "select s_comment.c_id,s_comment.c_content,s_comment.c_date,s_comment.u_no,user.u_name from s_comment,user where s_comment.s_id = ? and user.u_no = s_comment.u_no;";
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setInt(1, sId);
			ResultSet rs = pstmt.executeQuery();
			List<Comment> comments = new ArrayList<Comment>();
			while(rs.next())
			{
				Comment comment = new Comment();
				comment.setcId(rs.getInt(1));
				comment.setContent(rs.getString(2));
				comment.setDate(rs.getString(3));
				comment.setuNo(rs.getInt(4));
				comment.setuName(rs.getString(5));
				comments.add(comment);
			}
			sendObject(comments, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Constructor of the object.
	 */
	public GetComments() {
		super();
	}

}
